//
//  TableTextViewViewCell.m
//  FCTools
//
//  Created by WJC on 2018/10/25.
//  Copyright © 2018 wjc. All rights reserved.
//

#import "TableTextViewViewCell.h"
#import "WJCDefine.h"
#import "Masonry.h"
@interface TableTextViewViewCell()<UITextViewDelegate>
@end
@implementation TableTextViewViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.noteTxtView=[UITextView new];
        self.noteLabel=[UILabel new];
        self.noteLabel.frame=CGRectMake(16, 12, 120, 20);
        self.noteLabel.text=@"";
        self.noteLabel.font=font14;
        self.noteLabel.textColor=HEXC(0x646262);
//        if (IphonePlus) {
//            self.noteLabel.font=font15;
//        }
        self.noteLabel.textColor=HEXC(0x232323);
        [self.contentView addSubview: self.noteTxtView];
        WJCSETLRViewBorderRadius(self.noteTxtView, 4);
        self.noteTxtView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.noteLabel];
        self.noteTxtView.frame=CGRectMake(16, 40, WJCScreenW-32, 60);
        WJCWeakSelf
        [self.noteTxtView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(12);
            make.top.equalTo(weakSelf.noteLabel.mas_bottom).mas_offset(4);
            make.bottom.and.right.mas_equalTo(-12);
        }];
        [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.top.mas_equalTo(4);
            make.height.mas_equalTo(16);
        }]; 
        self.noteTxtView.delegate=self;
        self.noteTxtplaceHolder=[[UILabel alloc]initWithFrame:CGRectMake(20, self.noteTxtView.frame.origin.y+4, self.noteTxtView.frame.size.width, 60)];
        self.noteTxtplaceHolder.font=font14;
        self.noteTxtplaceHolder.text= @"";
        self.placeHolder=self.noteTxtplaceHolder.text;
        
        self.noteTxtplaceHolder.textColor=HEXC(0xCCCCCC);
        [self.contentView addSubview:self.noteTxtplaceHolder];
        [self.noteTxtplaceHolder mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.noteTxtView.mas_left).mas_offset(4);
            make.top.equalTo(weakSelf.noteTxtView.mas_top).mas_offset(4);
            make.right.mas_equalTo(-16);
        }];
        self.noteTxtplaceHolder.numberOfLines=0;
        self.placeHolder=self.noteTxtplaceHolder.text;
        self.countLabel=[UILabel new];
        self.countLabel.frame=CGRectMake(WJCScreenW-78, self.noteTxtView.frame.origin.y+self.noteTxtView.frame.size.height+8, 70, 15);
        self.countLabel.text=@"";
        self.countLabel.font=font12;
        self.countLabel.hidden=YES;
//        if (IphonePlus) {
//            self.noteLabel.font=font15;
//            self.noteTxtplaceHolder.font=font15;
//            self.countLabel.font=font13;
//        }
        self.countLabel.textColor=HEXC(0x646262);
        [self.contentView addSubview: self.countLabel];
        [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-2);
            make.right.mas_equalTo(-16);
        }];
        
    }
    return self;
}
-(void)setMaxCount:(NSInteger)maxCount{
    self.countLabel.hidden=NO;
    _maxCount=maxCount;
    if (maxCount>0) {
        self.countLabel.text=[NSString stringWithFormat:@"%lu/%ld",(unsigned long)self.noteTxtView.text.length,(long)self.maxCount];
    }
}
-(void)setfit{
    [self.noteLabel sizeToFit];
    [self.countLabel sizeToFit];
    if (self.noteLabel.text.length<1) {
        [self.noteTxtView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.top.mas_equalTo(8);
            make.bottom.and.right.mas_equalTo(-16);
        }];
    }else{
        WJCWeakSelf
        [self.noteTxtView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(12);
            make.top.equalTo(weakSelf.noteLabel.mas_bottom).mas_offset(4);
            make.bottom.and.right.mas_equalTo(-12);
        }];
    }
}
- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length<1) {
        self.noteTxtplaceHolder.text=self.placeHolder;
    }
    else{
        self.noteTxtplaceHolder.text=@"";
    }
    if (self.maxCount>0) {
        if (textView.text.length>self.maxCount) {
            textView.text =[textView.text substringToIndex:self.maxCount];
            self.countLabel.textColor=[UIColor redColor];
        }
        else self.countLabel.textColor=HEXC(0x646262);
        self.countLabel.text=[NSString stringWithFormat:@"%lu/%ld",(unsigned long)textView.text.length,(long)self.maxCount];
    }
    
    
    if (self.returnBlock) {
        self.returnBlock(textView);
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    if (self.returnEndBlock) {
        self.returnEndBlock(textView);
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
